package com.moxiao.yfjh.module.blind.controller.admin.notifymessage;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;
import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;
import com.moxiao.yfjh.module.blind.controller.admin.notifymessage.vo.BlindNotifyMessageCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.notifymessage.vo.BlindNotifyMessageExcelVO;
import com.moxiao.yfjh.module.blind.controller.admin.notifymessage.vo.BlindNotifyMessageExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.notifymessage.vo.BlindNotifyMessagePageReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.notifymessage.vo.BlindNotifyMessageRespVO;
import com.moxiao.yfjh.module.blind.controller.admin.notifymessage.vo.BlindNotifyMessageUpdateReqVO;
import com.moxiao.yfjh.module.blind.convert.notifymessage.BlindNotifyMessageConvert;
import com.moxiao.yfjh.module.blind.dal.dataobject.notifymessage.BlindNotifyMessageDO;
import com.moxiao.yfjh.module.blind.service.notifymessage.BlindNotifyMessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "管理后台 - 相亲通知消息")
@RestController
@RequestMapping("/blind/notify-message")
@Validated
public class BlindNotifyMessageController {

    @Resource
    private BlindNotifyMessageService notifyMessageService;

    @PostMapping("/create")
    @Operation(summary = "创建相亲通知消息")
    @PreAuthorize("@ss.hasPermission('blind:notify-message:create')")
    public CommonResult<Long> createNotifyMessage(@Valid @RequestBody BlindNotifyMessageCreateReqVO createReqVO) {
        return success(notifyMessageService.createNotifyMessage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新相亲通知消息")
    @PreAuthorize("@ss.hasPermission('blind:notify-message:update')")
    public CommonResult<Boolean> updateNotifyMessage(@Valid @RequestBody BlindNotifyMessageUpdateReqVO updateReqVO) {
        notifyMessageService.updateNotifyMessage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除相亲通知消息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blind:notify-message:delete')")
    public CommonResult<Boolean> deleteNotifyMessage(@RequestParam("id") Long id) {
        notifyMessageService.deleteNotifyMessage(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得相亲通知消息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blind:notify-message:query')")
    public CommonResult<BlindNotifyMessageRespVO> getNotifyMessage(@RequestParam("id") Long id) {
        BlindNotifyMessageDO notifyMessage = notifyMessageService.getNotifyMessage(id);
        return success(BlindNotifyMessageConvert.INSTANCE.convert(notifyMessage));
    }

    @GetMapping("/list")
    @Operation(summary = "获得相亲通知消息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blind:notify-message:query')")
    public CommonResult<List<BlindNotifyMessageRespVO>> getNotifyMessageList(@RequestParam("ids") Collection<Long> ids) {
        List<BlindNotifyMessageDO> list = notifyMessageService.getNotifyMessageList(ids);
        return success(BlindNotifyMessageConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得相亲通知消息分页")
    @PreAuthorize("@ss.hasPermission('blind:notify-message:query')")
    public CommonResult<PageResult<BlindNotifyMessageRespVO>> getNotifyMessagePage(@Valid BlindNotifyMessagePageReqVO pageVO) {
        PageResult<BlindNotifyMessageDO> pageResult = notifyMessageService.getNotifyMessagePage(pageVO);
        return success(BlindNotifyMessageConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出相亲通知消息 Excel")
    @PreAuthorize("@ss.hasPermission('blind:notify-message:export')")
    @OperateLog(type = EXPORT)
    public void exportNotifyMessageExcel(@Valid BlindNotifyMessageExportReqVO exportReqVO,
                                         HttpServletResponse response) throws IOException {
        List<BlindNotifyMessageDO> list = notifyMessageService.getNotifyMessageList(exportReqVO);
        // 导出 Excel
        List<BlindNotifyMessageExcelVO> datas = BlindNotifyMessageConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "相亲通知消息.xls", "数据", BlindNotifyMessageExcelVO.class, datas);
    }

}
